package com.shxt.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shxt.PageBean;
import com.shxt.model.Company;
import com.shxt.service.CompanyService;

@Controller
public class CompanyController {
    @Autowired
    private CompanyService companyService;
    //查询所有数据以及全字段查询
    @RequestMapping("com/list")
    public String list(String query,Company company,Map<String , Object> map,@RequestParam(required=false,defaultValue="1") int pages,@RequestParam(required=false,defaultValue="1") int p,HttpServletRequest request,String msg){

        map = PageBean.serverMap(map ,company , pages);
        
        map.put("query" , query);
        List<Company> comList = companyService.listCompany(map);
        
        Integer count = companyService.count(map);
        
        map = PageBean.map(map , company.getClass() , comList , count , pages);
        
        map.put("p" , p);
        map.put("count" , count);

        map.put("msg" , msg);
        return "/company/company";
        
    }
    
    //查看公司详情

    @RequestMapping("com/get/{company_id}")
    public String GetCompany(@PathVariable Integer company_id,Map<String , Object> map,HttpServletRequest request){

        Company company = companyService.load(company_id,request);
        map.put("company" , company);
        
        
        return "/company/companyDe";
    }
    //跳转到添加页面
    @RequestMapping("com/toAdd")
    public String toAdd(){
        
        
        
        return "/company/add";
    }
    //添加公司
    @PostMapping("com/add")
    public String add(Company company,HttpServletRequest request,Map<String , Object> map,String query){
        
        //临时添加，合并时删掉
        HttpSession session = request.getSession();
        session.setAttribute("userName" , "刘邦登");
        session.setAttribute("userId" , 1);
        
        
        int flg = companyService.add(company,request);
        String msg = new String();
        company = new Company();
        if(flg>0){
            msg="修改成功！";
        }else{
            msg = "修改失败，请重新尝试！";
        }

        return list(query , company,map  , 1 , 1, request,msg);
    }
    
    //跳转到修改页面
    @RequestMapping("com/toUpDate")
    public String toUpDate(Map<String , Object> map,HttpServletRequest request){
        HttpSession session = request.getSession();
        
        Company company = companyService.load(Integer.parseInt(session.getAttribute("company_id").toString()),request);
        map.put("company" , company);
        return "/company/upDate";
    }
    
    //修改公司信息
    @PostMapping("com/UpDate")
    public String UpDate(Company company,HttpServletRequest request,Map<String , Object> map,String query){
        
        int flg = companyService.update(company);
        String msg = new String();
        company = new Company();
        if(flg>0){
            msg="修改成功！";
        }else{
            msg = "修改失败，请重新尝试！";
        }

        return  list(query , company,map  , 1 , 1, request,msg);
    }
    //删除公司信息
    @RequestMapping("com/del")
    public String del(String company_ids,HttpServletRequest request, Map<String , Object> map,String query,Company company){
        
        int flg = companyService.delete(company_ids);
        String msg = new String();
        if(flg>0){
            msg="删除成功！";
        }else{
            msg = "删除失败，请重新尝试！";
        }

        return list(query , company,map  , 1 , 1, request,msg);
        
    }

}
